﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MethodWorx.CMS.Content;
using MethodWorx.CMS.Administration;
using MethodWorx.CMS.Membership;
using MethodWorx.CMS.Services;

namespace MethodWorx.CMS.UI.Models
{
    public class PreviewModel
    {

        public Page Page { get; private set; }
        public string Url { get; private set; }

        public PreviewModel(UserService users, ApplicationTypeService application, Page page, string key,string host)
        {
            this.Url = application.GetPagePreviewUrl(page);
            if (this.Url.Contains("?"))
                this.Url += "&preview=" + GetPreviewSessionKey(users, key, host);
            else
                this.Url += "?preview=" + GetPreviewSessionKey(users, key, host);


            this.Page = page;
        }



        private string GetPreviewSessionKey(UserService users, string key, string host)
        {
            PreviewSession previewSession = null; ;

            if (key != null)
                previewSession = users.GetPreviewSession(Guid.Parse(key));

            if (previewSession == null)
                previewSession = users.CreatePreviewSession(Guid.Empty, host);

            return previewSession.Id.ToString();
        }
    }
}